package com.lwj.liwanjia.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.TypeAdapter;
import com.lwj.liwanjia.config.OnItemClickListener;

import java.util.List;

/**
 *选择物品类型
 */

public class WupinTypeDialog extends Dialog {
    private Context context;
    private List<String> list;
    private RecyclerView rlvType;
    private int checkPosition = -1;
    private TextView tvSure;

    public WupinTypeDialog(@NonNull Context context, List<String> list) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wupin_type_layout);
        initView();
    }

    private void initView() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.dialog_animation);
        setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = params.MATCH_PARENT;
        params.height = params.WRAP_CONTENT;
        window.setAttributes(params);
        rlvType = (RecyclerView) findViewById(R.id.rlv_type);
        tvSure = (TextView) findViewById(R.id.tv_sure);
        FlexboxLayoutManager manager = new FlexboxLayoutManager();
        //设置主轴排列方式
        manager.setFlexDirection(FlexDirection.ROW);
        //设置是否换行
        manager.setFlexWrap(FlexWrap.WRAP);
        manager.setAlignItems(AlignItems.STRETCH);//可伸缩
        manager.setJustifyContent(JustifyContent.FLEX_START);
        rlvType.setLayoutManager(manager);
        final TypeAdapter adapter = new TypeAdapter(list, context);
        adapter.setCheckPosition(checkPosition);
        rlvType.setAdapter(adapter);
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                checkPosition = position;
                adapter.setCheckPosition(position);
                adapter.notifyDataSetChanged();
            }
        });
    }

    public TextView getSure() {
        return tvSure;
    }

    public int getCheckPosition() {
        return checkPosition;
    }
}


